//
//  registerNextViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/10/18.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "registerNextViewController.h"
#import "schoolListView.h"
#import <LGAlertView.h>
#import <AFNetworking.h>
#import "SVProgressHUD.h"
#import <MJExtension.h>
#import "schoolModel.h"
#import "regionListView.h"
#import "MBProgressHUD+MJ.h"
@interface registerNextViewController () <UIPickerViewDataSource,UIPickerViewDelegate,schoolListViewDelegate,regionListViewDelegate>
/** 文本框密码*/
@property (weak, nonatomic) IBOutlet UITextField *textPassword;
/** 文本框姓名*/
@property (weak, nonatomic) IBOutlet UITextField *textName;
/** 文本框性别*/
@property (weak, nonatomic) IBOutlet UITextField *textGender;
/** 文本框学校*/
@property (weak, nonatomic) IBOutlet UITextField *textSchool;
/** 文本框校区*/
@property (weak, nonatomic) IBOutlet UITextField *textCampus;
/** 文本框邀请码*/
@property (weak, nonatomic) IBOutlet UITextField *textCode;
/** 保存学校的数组*/
@property (nonatomic, strong) NSArray *arrCatogeries;
/** 学校列表*/
@property (nonatomic, strong) schoolListView *schoolListView;

/** 校区列表*/
@property (nonatomic, strong) regionListView *regionListView;
// 性别的pick
@property (strong,nonatomic) UIPickerView *genderPick;

// 展示学校的tableView
@property (nonatomic, strong) UITableView *schololTableView;
// 展示校区的tableView
@property (nonatomic, strong) UITableView *regionTableView;

/** 管理者 */
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@end

@implementation registerNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
}
// 选择性别
- (IBAction)btnGenderClick:(id)sender {
    LGAlertView *alert=[[LGAlertView alloc]initWithViewAndTitle:@"性别" message:@"" style:LGAlertViewStyleAlert view:self.genderPick buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) {
        
        if ([self.genderPick selectedRowInComponent:0] == 0) {
            self.textGender.text = @"男";
        }else if([self.genderPick selectedRowInComponent:0] == 1){
            self.textGender.text = @"女";
        }else if([self.genderPick selectedRowInComponent:0] == 2){
            
        }
        
    } cancelHandler:^(LGAlertView *alertView) {
        
    } destructiveHandler:^(LGAlertView *alertView) {
        
    }];
    [alert showAnimated:YES completionHandler:^{
        
    }];

}


- (void)schoolListView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    self.textSchool.text = cell.textLabel.text;
}
- (void)regionListView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    self.textCampus.text = cell.textLabel.text;
}

// 选择学校
- (IBAction)btnSchoolClick:(id)sender {
    
    self.textCampus.text = nil;
    // 显示指示器
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",GYMRequeatHost,GYMGetSchoolRegionList];
    
    [self.manager GET:url parameters:nil success:^(NSURLSessionDataTask *task, NSDictionary *responseObject) {
        
        [SVProgressHUD dismiss];
        
        self.arrCatogeries = [schoolModel mj_objectArrayWithKeyValuesArray:responseObject[@"result"]];
        NSLog(@"%@",responseObject);
        // 创建学校列表
        [UIView animateWithDuration:0.25 animations:^{
            self.schoolListView = [schoolListView viewFromNib];
            
            self.schoolListView.modelArr = self.arrCatogeries;
            
            self.schoolListView.delegate = self;
            
            self.schoolListView.frame = CGRectMake(0, 0, GYMScreenW, GYMScreenH);
            
            [self.view addSubview:self.schoolListView];
        }];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [SVProgressHUD dismiss];
    }];
}
// 选择校区
- (IBAction)btnCampusClick:(id)sender {
    if (self.textSchool.text == nil) {
        return;
    }
    // 创建校区列表
    [UIView animateWithDuration:0.25 animations:^{
        self.regionListView = [regionListView viewFromNib];
        
        self.regionListView.schoolName = self.textSchool.text;
        
        self.regionListView.delegate = self;
        
        self.regionListView.modelArr = self.arrCatogeries;
        
        self.regionListView.frame = CGRectMake(0, 0, GYMScreenW, GYMScreenH);
        
        [self.view addSubview:self.regionListView];
    }];

    
}
// 邀请码
- (IBAction)btnCodeClick:(id)sender {
}
// 注册
- (IBAction)btnRegistClick:(id)sender {
    
    
    
    
    // 参数：
    // 手机号
    NSString *telNum = self.telNumber;
    // 密码
    NSString *password = self.textPassword.text;
    // 真实姓名
    NSString *name = self.textName.text;
    // 学校编号
    NSInteger schoolUniversity = 0;
    // 校区编号
    NSInteger schoolregion = 0;
    // 设备类型 (2)
    // 性别
    NSInteger gender = 0;
    if ([self.textGender.text isEqualToString:@"男"]) {
        gender = 1;
    }
    // 短信验证获得的凭证
    NSString *secretCode = self.result;
    
    for (int i = 0; i < self.arrCatogeries.count; i++) {
        schoolModel *model = self.arrCatogeries[i];
        if ([model.UNIVERSITY_NAME isEqualToString: self.textSchool.text]) {
            schoolUniversity = model.UNIVERSITY;
            if ([model.REGION_NAME isEqualToString:self.textCampus.text]) {
                schoolregion = model.REGION;
            }
        }
    }
    
    NSLog(@"schoolUniversity = %zd schoolregion = %zd",schoolUniversity,schoolregion);
    // 拼接url
    NSString *url = [NSString stringWithFormat:@"%@%@",GYMRequeatHost,GYMEndRegiste];
    // 发送请求
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"username"] = telNum;
    params[@"password"] = password;
    params[@"real_name"] = name;
    params[@"university"] = @(schoolUniversity);
    params[@"region"] = @(schoolregion);
    params[@"device_type"] = @(2);
    params[@"gender"] = @(gender);
    params[@"secret_code"] = secretCode;
    
    [self.manager POST:url parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        [MBProgressHUD showSuccess:@"注册成功"];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
        NSLog(@"response = %@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD showError:@"注册失败" toView:self.view];
        NSLog(@"error = %@",error);
    }];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


// 性别Picker返回的组数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
    
}
// 返回的row
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(pickerView.tag == 1){
        return 2;
    }else{
        return 0;
    }
    
}
// 显示的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView.tag == 1){
        if (row == 0) {
            return @"男";
        }
        else {
            return @"女";
        }
    }
    else {
        return @"";
    }
}




#pragma mark- getter && setter
//- (UITextField *)textSchool{
//    if (<#condition#>) {
//        <#statements#>
//    }
//}



- (UIPickerView *)genderPick
{
    if (!_genderPick) {
        _genderPick = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    }
    _genderPick.delegate = self;
    _genderPick.dataSource = self;
    _genderPick.tag = 1;
    return _genderPick;
}


- (AFHTTPSessionManager *)manager{
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
        //申明返回的结果是json类型
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        //申明请求的数据是json类型
        _manager.requestSerializer=[AFJSONRequestSerializer serializer];
    }
    return _manager;
}


@end
